import flask
from mostone import mostone
from mostone.configs import config
import os
import subprocess
import flask_cors
import mowang
import json

mostone_api = flask.Blueprint("mostone_api", __name__)

MIYO_IPA_DIR        = "/Users/mac/Documents/ipas/miyo/"
LUKA_IPA_DIR        = "/Users/mac/Documents/ipas/luka/"
SABA_IPA_DIR        = "/Users/mac/Documents/ipas/saba/"
MostOne_IPA_DIR     = "/Users/mac/Documents/ipas/mw/"


@mostone_api.route("/command/")
@mostone_api.route("/command/<command>")
def execute_command(command=None):
    print(flask.request, command)
    if command is None:
        commandurl = flask.request.host_url + flask.url_for("mostone_api.execute_command")
        return flask.render_template("command.html", info={"commandurl": commandurl})
    else:
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        output, error = process.communicate()
        data = {
            "code": process.returncode,
            "msg": output.decode("utf-8"),
            "error": error.decode("utf-8")
        }
        return flask.render_template("command.html", info={"isresult": True, "result": json.dumps(data)})

@mostone_api.route("/mostone/api/scp/<name>")
@flask_cors.cross_origin()
def scp(name):
    print(flask.request.headers)
    if name == "mimi":
        mowang.foundation.util.excute_shell("sshpass -p '1234' scp ~/Desktop/LocalIpas/mimi/MMVoice.ipa mowang@192.168.199.121:/Users/mowang/.jenkins/workspace/MIMI_iOS/JenkinsSpeed/MMVoice.ipa", pipe_out=False)

    return {
        "code": 200,
        "msg": "success"
    }

###生成manifest文件，需要post对应的参数
# {
# 	"type": "release",
# 	"version": "4.0.0",
# 	"app": "luka"
# }
@mostone_api.route("/mostone/api/manifest/", methods=["post"])
def api_generate_manifest():
    json = flask.request.json
    type = json["type"]
    app = json["app"]
    version = json["version"]
    is_debug = type == "debug"
    print("参数:", json)
    mos = mostone.Mostone()
    if app == "miyo":
        data = config.miyo_manifest_data(version=version, debug=is_debug)
        filepath = MIYO_IPA_DIR
    else:
        data = config.luka_manifest_data(version=version, debug=is_debug)
        filepath = LUKA_IPA_DIR
    if is_debug:
        filepath = filepath + "debug/"
    else:
        filepath = filepath + "release/"
    filepath = filepath + "manifest.plist"
    mos.generate_manifest(data=data, filepath=filepath)

    return {
        "code": 200,
        "msg": "success"
    }

###拷贝ipa文件到对应的目录
# {
# 	"type": "release",
# 	"app": "luka"
# }
#sshpass -p '1234' scp ~/Documents/ipas/mimi/release/MMVoice.ipa mowang@192.168.199.121:/Users/mowang/Documents/ipas/mimi/release/MMVoice.ipa
@mostone_api.route("/mostone/api/getipa/", methods=["post"])
def api_get_some_ipa():
    json = flask.request.json
    type = json["type"]
    app = json["app"]
    is_debug = type == "debug"
    if app == "miyo":
        if is_debug:
            filepath = MIYO_IPA_DIR + "debug/MIYO.ipa"
            source = "192.168.199.106:/Users/modev/.jenkins/workspace/MOKA_iOS/JenkinsSpeed/MIYO.ipa"
        else:
            filepath = MIYO_IPA_DIR + "release/MIYO.ipa"
            source = "192.168.199.106:/Users/modev/Documents/ios/miyo/release/MIYO.ipa"
    else:
        if is_debug:
            filepath = LUKA_IPA_DIR + "debug/LUKA.ipa"
            source = "192.168.199.106:/Users/modev/.jenkins/workspace/BeiBei_iOS/JenkinsSpeed/BBVoice.ipa"
        else:
            filepath = LUKA_IPA_DIR + "release/LUKA.ipa"
            source = "192.168.199.106:/Users/modev/Documents/ios/luka/release/LUKA.ipa"

    cmd = "scp " + source + " " + filepath
    print("脚本：", cmd)
    p = subprocess.Popen(cmd, shell=True)
    try:
        p.wait()
    except:
        print("执行出错")
        return {
            "code": 500,
            "msg": "failure"
        }

    return {
        "code": 200,
        "msg": "success"
    }
    
@mostone_api.route("/mostone/api/test/", methods=["post", "get"])
def api_test():
    json = flask.request.headers
    print(json)
    return {
        "code": 200,
        "msg": "success"
    }
    
@mostone_api.route("/mostone/api/testparam/", methods=["get"])
def api_param_test():
    param = flask.request.args.to_dict()
    headers = flask.request.headers
    print(param)
    print(headers)
    # return {
    #     "code": 200,
    #     "data": "this is data",
    #     "msg": "success..."
    # }
    return "this is data"
